﻿using System;
using MediaBrowserSL.RIA.MediaBrowserServiceReference;
using System.Collections.Generic;

namespace MediaBrowserSL.RIA.Services
{
    public class AuthenticationRiaServices : IAuthenticationRiaServices
    {
        #region Private Member
        private AuthenticationServiceClient client;
        private Action<User, Exception> existUserCallback;
        private Action<int, Exception> createUserCallback;
        private Action<bool, Exception> existLoginCallback;
        private Action<User, Exception> updateStatutCallback;
        #endregion

        public AuthenticationRiaServices()
        {
            client = new AuthenticationServiceClient();
        }

        #region ExistUser

        public void ExistUser(Action<User, Exception> existUserCallback, string Login, string password)
        {
            this.existUserCallback = existUserCallback;
            client.ExistUserCompleted += client_ExistUserCompleted;
            client.ExistUserAsync(Login, password);
        }

        void client_ExistUserCompleted(object sender, ExistUserCompletedEventArgs e)
        {
            client.ExistUserCompleted -= client_ExistUserCompleted;
            if (e.Error == null) { this.existUserCallback(e.Result, e.Error); } else { this.existUserCallback(null, e.Error); }
        }

        #endregion

        #region CreateUser

        public void CreateUser(Action<int, Exception> createUserCallback, string firstName, string lastName, string Login, string password)
        {
            this.createUserCallback = createUserCallback;
            client.CreateUserCompleted += client_CreateUserCompleted;
            client.CreateUserAsync(firstName, lastName, Login, password);
        }

        void client_CreateUserCompleted(object sender, CreateUserCompletedEventArgs e)
        {
            client.CreateUserCompleted += client_CreateUserCompleted;
            if (e.Error == null) { this.createUserCallback(e.Result, e.Error); } else { this.createUserCallback(0, e.Error); }
        }

        #endregion

        #region UpdateStatut

        public void UpdateStatut(Action<User, Exception> updateStatutCallback, User u)
        {
            this.updateStatutCallback = updateStatutCallback;
            client.UpdateStatutCompleted += client_UpdateStatutCompleted;
            client.UpdateStatutAsync(u);
        }

        void client_UpdateStatutCompleted(object sender, UpdateStatutCompletedEventArgs e)
        {
            client.UpdateStatutCompleted -= client_UpdateStatutCompleted;
            if (updateStatutCallback != null)
            {
                if (e.Error == null) { this.updateStatutCallback(e.Result, e.Error); } else { this.updateStatutCallback(null, e.Error); }
            }
        }

        #endregion

        #region ExistLogin

        public void ExistLogin(Action<bool, Exception> existLoginCallback, string Login)
        {
            this.existLoginCallback = existLoginCallback;
            client.ExistLoginCompleted += client_ExistLoginCompleted;
            client.ExistLoginAsync(Login);
        }

        void client_ExistLoginCompleted(object sender, ExistLoginCompletedEventArgs e)
        {
            client.ExistLoginCompleted -= client_ExistLoginCompleted;
            if (e.Error == null) { this.existLoginCallback(e.Result, e.Error); } else { this.existLoginCallback(false, e.Error); }
        }

        #endregion

    }
}
